package auditoryUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;

public class ConfigurationChooser extends JFrame {

	
	private JButton button;
	
	private final String filePath = "./configuration/";
	
	private JList list;
	
	
	
	public ConfigurationChooser(final SimpleDriver sd) {//
		super("Conditions");
		list = new JList();
		getContentPane().add(GetConfigurationFileList(), BorderLayout.NORTH);
		
		button = new JButton("Choose");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//changeExperimentCondition(list.getSelectedIndex());
				String condition = (String)list.getSelectedValue();
				sd.loadConfiguration(filePath 
						+ condition, condition.substring(0,condition.indexOf(".xml")));
		}});
		getContentPane().add(button, BorderLayout.SOUTH);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				//ui.EndExperiment();
			}
		});
		int height = list.getModel().getSize();
		setSize(200,150);
		setVisible(true);
		setLocation(0, 0);
	}
	
	private JList GetConfigurationFileList(){
		File tmp = new File(filePath);
		list.setListData(tmp.list(new ConfigurationFileFilter()));
		return list;
	}
	
	public String getSelectedFile(){
		return filePath + (String)list.getSelectedValue();
	}
	
	public void setCurrentCondition(int i) {
		list.setSelectedIndex(i);
	}
	
//	public static void main(String[] args){
//		ConfigurationChooser cc = new ConfigurationChooser();
//	}
	
//	public void changeExperimentCondition(final int i) {
//		Thread t = new Thread() {
//			public void run() {
//				ui.EndCondition(i, "CONDITION_JUMP");			
//			}
//		};
//		t.start();
//		setState(this.ICONIFIED);
//	}
	
	private class ConfigurationFileFilter implements FilenameFilter{
		
		public boolean accept(File dir, String name){
			if (name.lastIndexOf(".xml") +4!= name.length()){
				return false;
			}
			return true;
		}
	}
	
}
